package com.anolesoft.yr.mbus.part.mapper;

import com.anolesoft.yr.mbus.part.entity.PartBomDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/15 16:11
 */
@Mapper
@Deprecated
public interface PartBomMapper {
    @Insert("insert into part_bom (" +
            "objuid, " +
            "fac_code, " +
            "parent_part_model_uid, " +
            "part_model_uid, " +
            "quantity, " +
            "c_user_uid, " +
            "c_time, " +
            "m_user_uid, " +
            "m_time, " +
            "del_flag, " +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{parentPartModelUid,jdbcType=VARCHAR}," +
            "#{partModelUid,jdbcType=VARCHAR}," +
            "#{quantity,jdbcType=INTEGER}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(PartBomDO partBomDO);

    @Update("update part_bom set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "parent_part_model_uid = #{parentPartModelUid,jdbcType=VARCHAR}," +
            "part_model_uid = #{partModelUid,jdbcType=VARCHAR}," +
            "quantity = #{quantity,jdbcType=INTEGER}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR} " +
            " where objuid = #{objuid,jdbcType=VARCHAR}")
    int update(PartBomDO partBomDO);

    @Update("update part_bom set del_flag = 1 where objuid = #{id}")
    int delete(@Param("id") String id);

    @Results(id = "PartBomResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "parent_part_model_uid", property = "parentPartModelUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "part_model_uid", property = "partModelUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "quantity", property = "quantity", jdbcType = JdbcType.INTEGER),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "parent_part_model_uid", property = "parentPartModel", one = @One(select = "com.anolesoft.yr.mbus.part.mapper.PartModelMapper.selectOneById",fetchType = FetchType.LAZY)),
            @Result(column = "part_model_uid", property = "partModel", one = @One(select = "com.anolesoft.yr.mbus.part.mapper.PartModelMapper.selectOneById",fetchType = FetchType.LAZY)),
    })
    @Select("select " +
            "objuid, fac_code, parent_part_model_uid, part_model_uid, quantity, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark" +
            " from part_bom where del_flag = 0 and objuid = #{id,jdbcType=VARCHAR}")
    PartBomDO selectOneById(@Param("id") String id);

    @Select("select " +
            "objuid, fac_code, parent_part_model_uid, part_model_uid, quantity, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark" +
            " from part_bom where del_flag = 0")
    @ResultMap("PartBomResultMap")
    List<PartBomDO> selectAll();

    @Select("select " +
            "objuid, fac_code, parent_part_model_uid, part_model_uid, quantity, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark" +
            " from part_bom where del_flag = 0")
    @ResultMap("PartBomResultMap")
    List<PartBomDO> selectPageAll(Page<PartBomDO> page);

    @Select("<script>" +
            "select " +
            "objuid, fac_code, parent_part_model_uid, part_model_uid, quantity, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark" +
            " from part_bom where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.parentPartModelUid !=null and o.parentPartModelUid != '' \">" +
            "   and parent_part_model_uid = #{o.parentPartModelUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partModelUid !=null and o.partModelUid != '' \">" +
            "   and part_model_uid = #{o.partModelUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    @ResultMap("PartBomResultMap")
    List<PartBomDO> selectList(@Param("o") PartBomDO partBomDO);

    @Select("<script>" +
            "select " +
            "objuid, fac_code, parent_part_model_uid, part_model_uid, quantity, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark" +
            " from part_bom where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.parentPartModelUid !=null and o.parentPartModelUid != '' \">" +
            "   and parent_part_model_uid = #{o.parentPartModelUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partModelUid !=null and o.partModelUid != '' \">" +
            "   and part_model_uid = #{o.partModelUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    @ResultMap("PartBomResultMap")
    List<PartBomDO> selectPageList(@Param("o") PartBomDO partBomDO, Page<PartBomDO> page);
}
